#include "server.h"

#include <brpc/controller.h>
#include <brpc/server.h>

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

EchoServiceImpl::EchoServiceImpl()
{
}

EchoServiceImpl::~EchoServiceImpl()
{
}

void EchoServiceImpl::Echo(google::protobuf::RpcController *controller,
                           const example::EchoRequest      *request,
                           example::EchoResponse           *response,
                           google::protobuf::Closure       *done)
{
    // RAII，确保在函数退出时调用done->Run()
    brpc::ClosureGuard done_guard(done);
    brpc::Controller  *cntl = static_cast<brpc::Controller *>(controller);

    cntl->set_after_rpc_resp_fn(std::bind(&EchoServiceImpl::CallAfterRpc, this, _1, _2, _3));

    LOG(INFO) << "Received request: " << request->message();
    response->set_message("echo " + request->message());
}

void EchoServiceImpl::CallAfterRpc(brpc::Controller *cntl, const google::protobuf::Message *req, const google::protobuf::Message *res)
{
    LOG(INFO) << "req:" << req->DebugString();
    LOG(INFO) << "res:" << res->DebugString();
}
